<template>
  <div class="app-container">
<!--    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">-->
<!--      <el-form-item label="发送成功数量" prop="successNum">-->
<!--        <el-input-->
<!--          v-model="queryParams.successNum"-->
<!--          placeholder="请输入发送成功数量"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="发送失败数量" prop="failNum">-->
<!--        <el-input-->
<!--          v-model="queryParams.failNum"-->
<!--          placeholder="请输入发送失败数量"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item>-->
<!--        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>-->
<!--        <el-button icon="Refresh" @click="resetQuery">重置</el-button>-->
<!--      </el-form-item>-->
<!--    </el-form>-->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['edu:allSms:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['edu:allSms:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['edu:allSms:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['edu:allSms:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="allSmsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="类型" align="center" prop="type" >
        <template #default="scope">
          <span v-if="scope.row.type == 1">短信</span>
          <span v-if="scope.row.type == 2">APP</span>
        </template>
      </el-table-column>
      <el-table-column label="发送内容" align="center" prop="content" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="发送成功数量" align="center" prop="successNum" />
      <el-table-column label="发送失败数量" align="center" prop="failNum" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['edu:allSms:edit']">修改</el-button>
<!--          <el-button link type="primary" icon="Edit" @click="handleSelectMan(scope.row)" v-hasPermi="['edu:allSms:edit']">分配人员</el-button>-->
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['edu:allSms:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改发送短信配置对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="allSmsRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="类型">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option key="1" value="短信" text="短信">
              短信
            </el-option>
            <el-option key="2" value="APP" text="APP">
              APP
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发送内容">
          <el-input type="textarea" v-model="form.content" :rows="10"/>
          teacherName为固定内容，用于标识老师名字所在位置
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入" />
        </el-form-item>

        <el-button v-if="form.id" type="primary" @click="selectMan">添加发送人员</el-button>
        <custom-table v-if="form.id" :tableData="selectList" :columns="columnSelectList" height="300px" :total="selectTotal">
        </custom-table>
<!--        <el-form-item label="发送成功数量" prop="successNum">-->
<!--          <el-input v-model="form.successNum" placeholder="请输入发送成功数量" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="发送失败数量" prop="failNum">-->
<!--          <el-input v-model="form.failNum" placeholder="请输入发送失败数量" />-->
<!--        </el-form-item>-->
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 添加配置短信人员对话框 -->
    <el-dialog title="选择发送短信人员" v-model="openManData" width="700px" append-to-body>
      <el-tabs v-model="activeName" @tab-click="handleTabClick">
        <el-button type="primary" @click="submitForm">确定选择选择的人员</el-button>
        <el-tab-pane label="学生" name="student">
          <custom-table :tableData="studentList" :columns="columnStudents" :total="studentTotal"
                    v-model:page="queryStudentParams.pageNum"
                    v-model:limit="queryStudentParams.pageSize"
                    show-index
                    @pagination="getStudentList"
          >
          </custom-table>
        </el-tab-pane>
        <el-tab-pane label="老师" name="teacher">
          <custom-table :tableData="teacherList" :columns="columnTeachers" :total="teacherTotal"
                        v-model:page="queryTeacherParams.pageNum"
                        v-model:limit="queryTeacherParams.pageSize"
                        show-index
                        @pagination="getTeacherList"
          >
          </custom-table>
        </el-tab-pane>
      </el-tabs>
    </el-dialog>
  </div>
</template>

<script setup name="AllSms">
import { listAllSms, getAllSms, delAllSms, addAllSms, updateAllSms } from "@/api/edu/allSms";
import { listStudent } from "@/api/edu/student";
import { listTeacher } from "@/api/edu/teacher";
import { listAllSmsUser, getAllSmsUser, delAllSmsUser, addAllSmsUser, updateAllSmsUser } from "@/api/edu/allSms";

const { proxy } = getCurrentInstance();

const allSmsList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const activeName = ref("student");
const openManData = ref(false);
const columnStudents = ref([{prop: 'xm', label: '姓名'},
  {prop: 'xh', label: '学号'},
  {prop: 'sznj', label: '所在年纪'},
  {prop: 'zymc', label: '专业名称'},
  {prop: 'bjmc', label: '班级名称'}]);
const queryStudentParams = ref({
  pageNum: 1,
  pageSize: 10
});
const studentList = ref([]);
const studentTotal = ref(0);
const columnTeachers = ref([{
  prop: 'xm', label: '姓名'},
  {prop: 'gh', label: '工号'},
  {prop: 'filed9', label: '职务名称'},
  {prop: 'dwh', label: '单位号'}]);
const queryTeacherParams = ref({
  pageNum: 1,
  pageSize: 10
});
const teacherList = ref([]);
const teacherTotal = ref(0);

const selectList = ref([]);
const columnSelectList = ref([{
  prop: 'xm', label: '姓名'},
  {prop: 'gh', label: '工号'},
  {prop: 'xh', label: '学号'},
  {prop: 'userId', label: '用户id'}]);
const selectTotal = ref(0);


const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    type: null,
    content: null,
    successNum: null,
    failNum: null
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询发送短信配置列表 */
function getList() {
  loading.value = true;
  listAllSms(queryParams.value).then(response => {
    allSmsList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    type: null,
    content: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null,
    successNum: null,
    failNum: null
  };
  proxy.resetForm("allSmsRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加发送短信配置";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getAllSms(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改发送短信配置";
  });


}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["allSmsRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateAllSms(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addAllSms(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除发送短信配置编号为"' + _ids + '"的数据项？').then(function() {
    return delAllSms(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('edu/allSms/export', {
    ...queryParams.value
  }, `allSms_${new Date().getTime()}.xlsx`)
}

function selectMan(){
  openManData.value = true;
  getStudentList();
  getTeacherList();
}

function getStudentList(){
  listStudent(queryStudentParams.value).then(response => {
    studentList.value = response.rows;
    studentTotal.value = response.total;
  });
}

function getTeacherList(){
  listTeacher(queryTeacherParams.value).then(response => {
    teacherList.value = response.rows;
    teacherTotal.value = response.total;
  });
}

function handleTabClick(){

}
getList();
</script>
